package pikes.samples.countries.ui.peak;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.support.RequestContext;

import pikes.ecma.SourceElements;
import pikes.peak.ClientSideScript;
import pikes.peak.EcmaScriptCode;
import pikes.peak.RequestIndependentProperty;
import pikes.peak.RequestProperty;
import pikes.peak.ScriptReference;
import pikes.uri.URI;

public abstract class BehaviourPage extends BaseCountriesPage {

	protected static final Log translatorLogger = LogFactory.getLog("Translator");

	private Collection<DynamicSourceElements> declaredCode = new LinkedList<DynamicSourceElements>();

	@Override
	protected final void setupClientSideScripts(Collection<ClientSideScript> scripts, Map model,
			RequestContext requestContext) throws Exception {

		RequestProperty<Collection<RequestProperty<URI>>> referenceURIs = getReferenceURIs();

		for (RequestProperty<URI> uri : referenceURIs.getValue(model, requestContext)) {
			scripts.add(new ScriptReference(uri));
		}

		RequestProperty<Collection<RequestProperty<SourceElements>>> customScripts = getCustomScripts();

		for (RequestProperty<SourceElements> customCode : customScripts.getValue(model, requestContext)) {
			EcmaScriptCode ecmaScript = new EcmaScriptCode(customCode.getValue(model, requestContext));
			ecmaScript.setScriptFormattingConfiguration(prettyJavaScript);
			scripts.add(ecmaScript);
		}

	}

	protected RequestProperty<Collection<RequestProperty<URI>>> getReferenceURIs() {
		Collection<RequestProperty<URI>> uriCollection = new LinkedList<RequestProperty<URI>>();
		setupFixedReferenceURIs(uriCollection);
		return new RequestIndependentProperty<Collection<RequestProperty<URI>>>(uriCollection);
	}

	protected RequestProperty<Collection<RequestProperty<SourceElements>>> getCustomScripts() {
		Collection<RequestProperty<SourceElements>> fixedSourceElementCollection = new LinkedList<RequestProperty<SourceElements>>();
		setupEcmascriptManually(fixedSourceElementCollection);

		return new RequestIndependentProperty<Collection<RequestProperty<SourceElements>>>(fixedSourceElementCollection);
	}

	protected void setupEcmascriptManually(Collection<RequestProperty<SourceElements>> scripts) {
		for (DynamicSourceElements sourceElements : declaredCode) {
			scripts.add(sourceElements);
		}
	}

	protected void setupFixedReferenceURIs(Collection<RequestProperty<URI>> referencesURIs) {

	}

	@Override
	protected final void translate() {
		translate(declaredCode);
	}

	protected void translate(Collection<DynamicSourceElements> declaredCode) {

	}
}
